<?php
namespace admin\dao;

use admin\dao\interfaces\IManagerDao;
use common\dao\CommonDao;
use herosphp\core\Loader;
use herosphp\utils\ArrayUtils;

Loader::import('admin.dao.interfaces.IManagerDao');

/**
 * admin(DAO)接口实现
 * @package admin\dao
 * @author yangjian<yangjian102621@gmail.com>
 */
class ManagerDao extends CommonDao implements IManagerDao {

    public function getItem($condition, $fields, $order)
    {
		$condition['del_status'] = 0;
        $item = parent::getItem($condition, $fields, $order);
        if ( !empty($item['role_id']) ) {
            $model = Loader::model("ManagerRole");
            $role = $model->getItem($item['role_id']);
            $item['role_name'] = $role['name'];
        }
        return $item;
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
		$conditions['del_status'] = 0;
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if ( !empty($items) ) {
            $ids = array();
            foreach ( $items as $val ) {
                $ids[] = $val['role_id'];
            }
            $model = Loader::model("ManagerRole");
            $roles = $model->getItems(array('id' => array('$in' => $ids)));
            $roles = ArrayUtils::changeArrayKey($roles, 'id');
            foreach ( $items as $key => $value ) {
                $items[$key]['role_name'] = $roles[$value['role_id']]['name'];
            }
        }

        return $items;
    }

	public function delete($id)
	{
		return  parent::update(array('del_status' => 1),$id);
	}

	public function deletes($conditions)
	{
		return parent::updates(array('del_status' => 1),$conditions);
	}
}
